package configer;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import common.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.HttpClient4;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class SqlServerInitConfiger {
    protected final static Logger logger = LoggerFactory.getLogger(SqlServerInitConfiger.class);         // 日志对象
    private Properties properties = new Properties();                                       // 本地配置对象

    // 数据库
    private String sqlServer_address;         // 数据库IP地址
    private int sqlServer_port;            // 数据库访问端口
    private String sqlServer_account;         // 数据库访问账户
    private String sqlServer_password;        // 数据库账户密码
    private String sqlServer_database;        // 数据库名称
    private String sqlServer_schema;          // 模式
    private String[] sqlServer_tableNames;       // 数据表名称
    private String sqlServer_dataUser;

    public SqlServerInitConfiger(String propertiesName) {

        // 尝试读取本地配置文件
        try {
            FileInputStream arg = new FileInputStream(System.getProperty("user.dir") + "/config/" + propertiesName);
            properties.load(arg);
            logger.info("加载 配置文件 <" + Const.PROPERTIES_NAME + "> 成功");
        } catch (IOException e) {
            logger.error("加载 配置文件 <" + Const.PROPERTIES_NAME + "> 错误 { " + e + " }");
        }
        // 读取本地配置文件的四个重要参数
        if ("".equals(properties.getProperty("sqlServer_address")) || "".equals(properties.getProperty("sqlServer_account")) ||
                "".equals(properties.getProperty("sqlServer_password")) || "".equals(properties.getProperty("sqlServer_database")) ||
                "".equals(properties.getProperty("sqlServer_schema")) || "".equals(properties.getProperty("sqlServer_tableName"))) {
            logger.error("配置文件中未设置 sqlServer_account/sqlServer_password/sqlServer_address/sqlServer_database/sqlServer_schema/sqlServer_tableName 的值，请核对！");
            System.exit(0);
        }
        sqlServer_account = properties.getProperty("sqlServer_account");
        sqlServer_password = properties.getProperty("sqlServer_password");
        sqlServer_address = properties.getProperty("sqlServer_address");
        sqlServer_database = properties.getProperty("sqlServer_database");
        sqlServer_schema = properties.getProperty("sqlServer_schema");
        sqlServer_tableNames = properties.getProperty("sqlServer_tableNames").split("\\,");
        sqlServer_port = "".equals(properties.getProperty("sqlServer_port")) ? Const.SQLSERVER_DATABASE_PORT : Integer.parseInt(properties.getProperty("sqlServer_port"));
        sqlServer_dataUser = properties.getProperty("sqlServer_dataUser");
    }

    public List<SqlServerConfiger> getConfigrs() {

        List<SqlServerConfiger> configers = new ArrayList<SqlServerConfiger>();

        // 遍历tableNames，组装url，调用接口查询元数据
        for (String tableName : sqlServer_tableNames) {

            JSONObject result = JSONUtil.parseObj(getFromHttp(sqlServer_dataUser + "-" + sqlServer_address + "-" + sqlServer_database + "-" + sqlServer_schema + "-" + tableName));
            if ("00000".equals(result.getStr("code"))) {
                result = JSONUtil.parseObj(result.getStr("data"));
            }

            // 实例化configer对象
            SqlServerConfiger sqlServerConfiger = new SqlServerConfiger(tableName, result, properties);
            configers.add(sqlServerConfiger);
        }
        logger.info("configers初始化完毕！");
        return configers;

    }

    private String getFromHttp(String url) {
        logger.info("向" + Const.GET_THE_INTERFACE_OF_METADATE + url + "请求元数据");
        return HttpClient4.doGet(Const.GET_THE_INTERFACE_OF_METADATE + url);
    }

}

